package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.MatDeteMapper;
import com.indusfo.spc.pojo.MatBom;
import com.indusfo.spc.pojo.MatDete;
import com.indusfo.spc.service.MatDeteService;
import com.indusfo.spc.vo.HintMessage;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author liub
 * @Date 2019/11/22 17:43
 * @Version 1.0
 */

@Service
public class MatDeteServiceImpl implements MatDeteService {

    /**
     * 声明日志的静态变量
     */
    private static final Logger logger = LoggerFactory.getLogger(MatDeteServiceImpl.class);

    @Resource
    private MatDeteMapper matDeteMapper;

    /**
     * 查询
     */
    @Override
    public JSONObject queryMatDete(MatDete matDete) {
        try {
            //获取页面的数据个数
            Integer pagesize = matDete.getPagesize();
            //获取查询的页数
            Integer pageindex = matDete.getPageindex();
            if (pagesize != null && pageindex != null) {
                matDete.setIncept(pagesize * (pageindex - 1));
            }
            //执行查询操作
            List<MatDete> matDeteList =new ArrayList<>();
            if(matDete.getMatFlowId()==null){
                return JSONObject.oK(HintMessage.QUERY_NULL, matDeteList, 0);
            }
            matDeteList = matDeteMapper.queryMatDete(matDete);

            //如果页数和页面数量都不为空的情况下进行计数
            if (matDeteList.isEmpty()) {
                return JSONObject.oK(HintMessage.QUERY_NULL, matDeteList, 0);
            } else {
                Integer count = matDeteMapper.countMatDete(matDete);
                return JSONObject.oK(HintMessage.QUERY_SUCCESS, matDeteList, count);
            }

        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());

        }
    }

    /**
     * 新增
     */
    @Override
    public JSONObject insertMatDete(MatDete matDete) {
        try {
            if (matDete.getMatDeteId() != null) {
                throw new ModifyFailedException(HintMessage.INSERT_ID);
            }
            //初始化基础数据
            if (matDete.getDataState() == null) {
                matDete.setDataState(1);
            }
            //判重
            checkParam(matDete);
            //执行新增操作
            Integer row = matDeteMapper.insertMatDete(matDete);
            if (row == 0) {
                throw new ModifyFailedException(HintMessage.INSERT_DEFEATED);
            }
            return JSONObject.oK(HintMessage.INSERT_SUCCESS);
        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }

    }

    /**
     * 修改
     */
    @Override
    public JSONObject updateMatDete(MatDete matDete) {
        try {
            if (matDete.getMatDeteId() == null) {
                throw new ParamsErrorException(HintMessage.UPDATE_ID);
            }
            //初始化基础数据
            if (matDete.getDataState() == null) {
                matDete.setDataState(1);
            }
            //判重
            checkParam(matDete);
            //执行修改操作
            int row = matDeteMapper.updateMatDete(matDete);
            if (row == 0) {
                throw new ModifyFailedException(HintMessage.UPDATE_DEFEATED);
            }
            return JSONObject.oK(HintMessage.UPDATE_SUCCESS);
        } catch (GlobalException e) {//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 删除
     */
    @Override
    public JSONObject deleteMatDete(List<Integer> matDeteIds, Integer dataState) {
        try {
            //空值判断
            if (dataState == null) {
                throw new ParamsErrorException(HintMessage.DATASTATE_NULL);
            }
            if (matDeteIds == null) {
                throw new ParamsErrorException(HintMessage.LIST_NULL);
            }
            //记录异常信息
            String msg = "";
            switch (dataState) {
                case 1:
                    msg = HintMessage.START_VALUE;
                    break;
                case 2:
                    msg = HintMessage.DELETE_VALUE;
                    break;
                case 3:
                    msg = HintMessage.STOP_VALUE;
                    break;
                default:
                    throw new ParamsErrorException(HintMessage.VIOLATION_OPERATION);
            }
            //执行删除操作
            int row = matDeteMapper.deleteMatDete(matDeteIds, dataState);
            //声明msg字符串，用来存抛出的异常
            if (row == 0) {
                msg = msg + HintMessage.DEFEATED_VALUE;
            } else {
                msg = msg + HintMessage.SUCCESS_VALUE;
            }
            return JSONObject.oK(msg);
        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 判重
     */
    public void checkParam(MatDete matDete) {

        //产品流程ID
        Integer matFlowId = matDete.getMatFlowId();
        //采集参数ID
        Integer deteId = matDete.getDeteId();

        //产品流程ID
        if (matFlowId == null) {
            throw new ParamsErrorException(HintMessage.MATFLOWID_NULL);
        }
        //采集参数ID
        if (deteId == null) {
            throw new ParamsErrorException(HintMessage.DETEID_NULL);
        }

        //上限
        String upperLimit = matDete.getUpperLimit();
        //下限
        String lowerLimit = matDete.getLowerLimit();
        //中心值
        String middleLimit = matDete.getMiddleLimit();

        //上下限数值判断
        if (org.springframework.util.StringUtils.isEmpty(upperLimit)) {
            throw new ParamsErrorException("请填写规格上限");
        }
        if (org.springframework.util.StringUtils.isEmpty(lowerLimit)) {
            throw new ParamsErrorException("请填写规格下限");
        }
        if (StringUtils.isEmpty(middleLimit)) {
            throw new ParamsErrorException("请填写规格中心值");
        }
        if (upperLimit.getBytes().length > 50) {
            throw new ParamsErrorException("规格上限超过规定长度");
        }
        if (lowerLimit.getBytes().length > 50) {
            throw new ParamsErrorException("规格下限超过规定长度");
        }
        if (middleLimit.getBytes().length > 50) {
            throw new ParamsErrorException("规格中心值超过规定长度");
        }
        if (Double.parseDouble(upperLimit) <= Double.parseDouble(lowerLimit)) {
            throw new ParamsErrorException("规格上限值小于规格下限值");
        }
        if (Double.parseDouble(middleLimit) < Double.parseDouble(lowerLimit)) {
            throw new ParamsErrorException("规格中心值小于规格下限值");
        }
        if (Double.parseDouble(middleLimit) > Double.parseDouble(upperLimit)) {
            throw new ParamsErrorException("规格中心值大于规格上限值");
        }

        // 执行判重操作
        Integer Counts = matDeteMapper.checkMatDete(matDete);
        if (Counts != 0) {
            throw new ParamsErrorException(HintMessage.REPEATING_DATA);
        }

    }

}
